package com.crawler.downloader;

import com.crawler.api.CrawlerDownloader;
import com.crawler.model.CrawlerPageInfo;
import com.crawler.model.CrawlerRequestInfo;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service("jsoupCrawlerDownloader")
@Slf4j
public class JsoupCrawlerDownloader implements CrawlerDownloader {


    @Override
    public CrawlerPageInfo download(CrawlerRequestInfo crawlerRequestInfo) {
        try {
            // 请求设置
            Connection conn = Jsoup.connect(crawlerRequestInfo.getUrl());
            if (crawlerRequestInfo.getParamMap() != null && !crawlerRequestInfo.getParamMap().isEmpty()) {
                conn.data(crawlerRequestInfo.getParamMap());
            }
            if (crawlerRequestInfo.getCookieMap() != null && !crawlerRequestInfo.getCookieMap().isEmpty()) {
                conn.cookies(crawlerRequestInfo.getCookieMap());
            }
            if (crawlerRequestInfo.getHeaderMap() != null && !crawlerRequestInfo.getHeaderMap().isEmpty()) {
                conn.headers(crawlerRequestInfo.getHeaderMap());
            }
            if (crawlerRequestInfo.getUserAgent() != null) {
                conn.userAgent(crawlerRequestInfo.getUserAgent());
            }
            if (crawlerRequestInfo.getReferrer() != null) {
                conn.referrer(crawlerRequestInfo.getReferrer());
            }
            conn.timeout(crawlerRequestInfo.getTimeoutMillis());
            conn.validateTLSCertificates(crawlerRequestInfo.isValidateTLSCertificates());
            conn.maxBodySize(0);    // 取消默认1M限制

            // 代理
            if (crawlerRequestInfo.getProxy() != null) {
                conn.proxy(crawlerRequestInfo.getProxy());
            }

            conn.ignoreContentType(true);
            conn.method(crawlerRequestInfo.isIfPost() ? Connection.Method.POST : Connection.Method.GET);

            // 发出请求
            Connection.Response resp = conn.execute();
            String pageSource = resp.body();
            CrawlerPageInfo crawlerPageInfo = new CrawlerPageInfo();
            crawlerPageInfo.setBody(pageSource);
            return crawlerPageInfo;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }


}
